var windowNameIndex = parent.layer.getFrameIndex(window.name); //获取窗口索引
$(function () {
    var jsonStr = '${payServiceInfoList}';
    if (jsonStr.length > 0) {
        var depositPayServiceArrayStr = $("#depositPayServiceArray", window.parent.document).val();
        if (depositPayServiceArrayStr.length > 0) {
            var depositPayServiceArray = JSON.parse(depositPayServiceArrayStr);
            var amount = $("#amount").val();
            var deductionAmount = $("#deductionAmount").val();
            var html = '';
            var earnestMoneyList = JSON.parse(jsonStr);
            for (var i = 0; i < earnestMoneyList.length; i++) {
                var obj = earnestMoneyList[i];
                var foldingPay = obj.foldingPay;
                for (var j = 0; j < depositPayServiceArray.length; j++) {
                    var depositObj = depositPayServiceArray[j];
                    if (depositObj.depositPayServiceId == obj.id) {
                        if (parseInt(obj.foldingPay) >= parseInt(depositObj.depositAmount)) {
                            foldingPay = parseInt(obj.foldingPay) - parseInt(depositObj.depositAmount)
                        }
                    }
                }
                html += '<div class="depositPayServices" style="border-bottom: 1px solid #DDDDDD;display: flex;padding: 7px 0;">' +
                    '<div style="flex: 1">' + obj.payServiceName + '</div><inpint class="depositPayServiceId" value="' + obj.payServiceId + '" /><input class="oldFoldingPay" type="hidden" value="' + obj.foldingPay + '" /><input class="id" type="hidden" value="' + obj.id + '" />' +
                    '<div class="depositBalance" style="flex: 1">' + foldingPay + '</div>' +
                    '<div style="flex: 1"><input class="depositAmount" onkeyup="this.value=this.value.replace(/\\D/g,\'\')" onafterpaste="this.value=this.value.replace(/\\D/g,\'\')" oninput="changeDepositAmount(this)" value="0" type="text" style="width: 80%"></div>' +
                    '</div>';
            }
            $("#earnestMoneyList").html(html);
        } else {
            var totaldDepositAmount = parseInt($("#totaldDepositAmount").val());
            var amount = $("#amount").val();
            var deductionAmount = $("#deductionAmount").val();
            var html = '';
            var earnestMoneyList = JSON.parse(jsonStr);
            for (var i = 0; i < earnestMoneyList.length; i++) {
                var obj = earnestMoneyList[i];
                var foldingPay = obj.foldingPay;
                html += '<div class="depositPayServices" style="border-bottom: 1px solid #DDDDDD;display: flex;padding: 7px 0;">' +
                    '<div style="flex: 1">' + obj.payServiceName + '</div><inpint class="depositPayServiceId" value="' + obj.payServiceId + '" /><input class="oldFoldingPay" type="hidden" value="' + obj.foldingPay + '" /><input class="id" type="hidden" value="' + obj.id + '" />' +
                    '<div class="depositBalance" style="flex: 1">' + foldingPay + '</div>' +
                    '<div style="flex: 1"><input class="depositAmount" onkeyup="this.value=this.value.replace(/\\D/g,\'\')" onafterpaste="this.value=this.value.replace(/\\D/g,\'\')" oninput="changeDepositAmount(this)" value="0" type="text" style="width: 80%"></div>' +
                    '</div>';
            }
            $("#earnestMoneyList").html(html);
        }
    } else {
        layer.msg('该患者没有交储值！', {time: 1000, icon: 2});
        setTimeout(function () {
            parent.layer.close(windowNameIndex);
        }, 2000);
    }
});

function changeDepositAmount(obj) {
    var depositAmount = parseInt($(obj).val());
    var depositBalance = parseInt($(obj).parent().parent().find(".oldFoldingPay").val());
    var amount = parseInt($("#amount").val());
    if (depositAmount > amount) {
        layer.msg('抵扣金额不能超过实收金额！', {time: 1000, icon: 2});
        return false;
    } else {
        $(obj).parent().parent().find(".depositBalance").html(depositBalance - depositAmount);
    }
};

function CommitDepositAmount() {
    var depositPayServiceList = $(".depositPayServices");
    var depositPayServices = [];
    var payServiceId = $("#payServiceId").val();
    var totalAmount = 0;
    var jsonStr = $("#depositPayServiceArray", window.parent.document).val();
    if (jsonStr.length > 0) {
        var depositPayServiceArray = JSON.parse(jsonStr);
        for (var i = 0; i < depositPayServiceList.length; i++) {
            var obj = depositPayServiceList[i];
            for (var j = 0; j < depositPayServiceArray.length; j++) {
                var depositObj = depositPayServiceArray[j];
                if (depositObj.depositPayServiceId == $(obj).find(".id").val()) {
                    var depositMoney = parseInt(depositObj.depositAmount) + parseInt($(obj).find(".depositAmount").val());
                    depositObj.depositAmount = depositMoney;
                } else {
                    var depositPayService = {};
                    depositPayService['depositPayServiceId'] = $(obj).find(".id").val();
                    depositPayService['depositAmount'] = $(obj).find(".depositAmount").val();
                    depositPayServiceArray.push(depositPayService)
                }
            }
            totalAmount = totalAmount + parseInt($(obj).find(".depositAmount").val());
        }
        var str = JSON.stringify(depositPayServiceArray);
        $("#depositPayServiceArray", window.parent.document).val(str);
    } else {
        for (var i = 0; i < depositPayServiceList.length; i++) {
            var obj = depositPayServiceList[i];
            var depositPayService = {};
            depositPayService['depositPayServiceId'] = $(obj).find(".id").val();
            depositPayService['depositAmount'] = $(obj).find(".depositAmount").val();
            depositPayServices.push(depositPayService);
            totalAmount = totalAmount + parseInt($(obj).find(".depositAmount").val());
        }
        var str = JSON.stringify(depositPayServices);
        $("#depositPayServiceArray", window.parent.document).val(str);
    }
    var totalPayService = 0;
    var totalRealAmount = 0;
    var deductionDealList = $(".deductionDealList", window.parent.document);
    for (var i = 0; i < deductionDealList.length; i++) {
        var deductionDeal = deductionDealList[i];
        if (payServiceId == $(deductionDeal).find(".payServiceId").val()) {
            $(deductionDeal).find(".depositAmount").val(totalAmount);
            var paySerivce = parseInt($(deductionDeal).find(".amount").html());
            if ($('#integral').is(':checked')) {
                var deductionAmount = parseInt($(deductionDeal).find(".deductionAmount").val());
                $(deductionDeal).find(".realAmount").html(paySerivce - totalAmount - deductionAmount);
            } else {
                $(deductionDeal).find(".realAmount").html(paySerivce - totalAmount);
            }
        }
        totalPayService = totalPayService + parseInt($(deductionDeal).find(".amount").html());
        totalRealAmount = totalRealAmount + parseInt($(deductionDeal).find(".realAmount").html());
    }
    $("#realTotalSum2", window.parent.document).val(totalPayService);
    $("#realTotalSum3", window.parent.document).val(totalRealAmount);
    parent.layer.close(windowNameIndex);
};